local Construct = require'constructs.construct'

local DockPlatform = {}
DockPlatform.mt = { __index = DockPlatform}
setmetatable(DockPlatform, Construct.mt)


function DockPlatform.initialize(x, y, x2, y2)
	local DockP = {}
	
	
	
	
	DockP.x = x
	DockP.y = y
	DockP.x2 = x2
	DockP.y2 = y2
	DockP.w = math.abs(x-x2)
	DockP.h = math.abs(y-y2)
	DockP.parts = {}
	DockP.layout = DockPlatform.plan_dock( DockP )

	
	DockP = Construct.new( DockP.layout, nil, x, y )
	setmetatable(DockP, DockPlatform.mt)
	DockP.x = x
	DockP.y = y
	DockP.x2 = x2
	DockP.y2 = y2
	DockP.w = math.abs(x-x2)
	DockP.h = math.abs(y-y2)
	DockP.type = "dock"
	
	print("DOCKPLATFORM w, h : " .. DockP.w .. " " .. DockP.h)
	DockP.offset_x = DockP.w-3
	DockP.offset_y = DockP.h
	
	
	
	return DockP

end


function DockPlatform.new( x, y, x2, y2)

	local o = DockPlatform.initialize(x, y, x2, y2)
	
	return o

end

function DockPlatform:plan_dock()
	
	local plan = {}
	
	for y = 1, self.h do
		plan[y] = {}
		--if not plan[y] then plan[y+1] = {} end
		for x = 1, self.w do
			if y == 1 and x ==2 then		
				
				plan[y][x] = AIRLOCK_TILE			
				
			elseif y == self.h and x == self.w-1 then
				plan[y][x] = AIRLOCK_TILE
				plan[y+1] = {}
				plan[y+1][x] = HOOK_TILE
			elseif x > 1 and y > 1 and x < self.w and y < self.h then
				plan[y][x] = FLOOR_TILE
			
			elseif y == 1 or x == 1 then
			
				plan[y][x] = WALL_TILE
			elseif y == self.h or x == self.w then
			
				plan[y][x] = WALL_TILE				
			end		
		end	
	end
	
	--table.insert(plan, 1, {})
	--table.insert(plan[1], 2, HOOK_TILE)
	
	for i = 1, #plan do
		for j = 1, #plan[2] do
			
				if not plan[i][j] then
					plan[i][j] = 0
				end
				
				
			
			--print("plan #"..i.." "..j.." "..plan[i][j])
		end
	end
	
	local new_plan = {}
	
	local new_height = self.h+2
	
	for y = 1, new_height do
		new_plan[y] = {}
		for x = 1, self.w do
		
			if y == 1 and x == 2 then
			
				new_plan[y][x] = HOOK_TILE
				
			elseif y == 1 then
			
				new_plan[y][x] = 0
			
			else
			
				new_plan[y][x] = plan[y-1][x]
				
				if y == new_height then
				
					--new_plan[y+1]={}
					--new_plan[y+1][x]=plan[y][x]
				end
				
			end
			
			
			--print("new_plan y: " .. y .. " x: "..x.." val: "..new_plan[y][x])
		
		
		end
		
	end
	
	
	return new_plan

end

function DockPlatform:hooker( const_a, const_b )

	local dock_hooks = self:get_hooks()
	local xa,ya = dock_hooks[1].prop:getLoc()
	local xb, yb = dock_hooks[2].prop:getLoc()
	
	local const_a_hooks, xc, yc, ca, aox, aoy
	
	if const_a then
		const_a_hooks = const_a:get_hooks()
		xc, yc = const_a_hooks[1].prop:getLoc()
		ca = const_a_hooks[1]
		aox = const_a.offset_x*TILE_SIZE
		aoy = const_a.offset_y*TILE_SIZE
		const_a.transform:setAttrLink(MOAITransform.INHERIT_TRANSFORM, dock_hooks[1].prop, MOAITransform.TRANSFORM_TRAIT)
	end
	
	local const_b_hooks, xd, yd, cb, box, boy
	if const_b then
		const_b_hooks = const_b:get_hooks()
		xd, yd = const_b_hooks[1].prop:getLoc()
		cb = const_b_hooks[1]
		box = const_b.offset_x*TILE_SIZE
		boy = const_b.offset_y*TILE_SIZE
		const_b.transform:setAttrLink(MOAITransform.INHERIT_TRANSFORM, dock_hooks[2].prop, MOAITransform.TRANSFORM_TRAIT)
	end	
	
	local r1 = dock_hooks[1].prop:getRot()
	local r2 = dock_hooks[2].prop:getRot()
	
	-- DOCKING FORMULAS!!!! HARD MATH DANGER ZONE !!!!
	if const_a then
		const_a:setPiv(xb, yb) 
	
		const_a:seekLoc(aox, aoy, const_a.dock_time) 	
		const_a:seekRot(180,  const_a.dock_time) 
	
		const_a:seekRot(0, const_a.dock_time)
		
	end
	
	if const_b then
		const_b:setPiv(xa, ya)
	
	
	-- LOOK AT THIS FUCKED UP CALCULATION
		const_b:seekLoc(-box-((self.w-3)*TILE_SIZE), -boy-(self.h+1)*TILE_SIZE, const_b.dock_time)
	end
	
	-- tie everything to the object and cleanup	
	if not self.docked_const then self.docked_const = {} end
	
	local iter = #self.docked_const+1
	
	if const_a then
		self.docked_const[iter] = const_a		
		const_a:toggleDock()
	end
	
	if const_b then
		iter = #self.docked_const+1
		self.docked_const[iter] = const_b
		const_b:toggleDock()
	end
	
	print("DOCKPLATFORM hooker vals " .. xa, ya, xb, yb, xc, yc, xd, yd)

end

function DockPlatform:unhook( const_a, const_b )
	
	if const_a then
		const_a.transform:clearAttrLink(MOAITransform.INHERIT_TRANSFORM)
		const_a:toggleDock()
	end
	
	if const_b then
		const_b.transform:clearAttrLink(MOAITransform.INHERIT_TRANSFORM)
		const_b:toggleDock()
	end	

end




return DockPlatform